const mongoose = require('mongoose');
const crypto = require('crypto');
const xss = require('xss');
const User = require('../models/user');
const Group = require('../models/group');
const Message = require('../models/message');
const { getJwt, testJwt } = require('../utils');

// 创建组
exports.create = async (ctx, next) => {
    const { name, creator } = ctx.request.fields;
    await new Promise((resolve, reject) => {
        Group.findOne({name}, function(err, data) {
            if (err) {
                reject(err);
            } else if (!data) {
                const group = new Group({
                    name, creator,
                    members: [creator]
                });
                group.save(function(err, data) {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(true);
                    }
                })
            } else {
                resolve(false);
            }
            
        })
    }).then(res => {
        if (res) {
            ctx.body = {
                reasonCode: 200,
                message: '创建成功'
            }
        } else {
            ctx.body = {
                reasonCode: 302,
                message: '群组已存在'
            }
        }
    }).catch(err => {
        ctx.body = err;
    })
}

// 加入组
exports.join = async (ctx, next) => {
    const { groupId, id } = ctx.request.fields;
    await new Promise((resolve, reject) => {
        Group.findOne({_id: groupId}, function(err, data) {
            if (err) {
                reject(err);
            } else if (!data) {
                resolve(404);
            } else {
                const index = data.members.findIndex(item => item === id);
                if (id > -1) {
                    resolve(302);
                } else {
                    data.members.push(id);
                    Group.findOneAndUpdate({_id: groupId}, { members: data.members }, function(err, data) {
                        if (err) {
                            reject(err);
                        } else {
                            resolve(200);
                        }
                    })
                }
            }
        })
    }).then(code => {
        if (code === 404) {
            ctx.body = {
                reasonCode: 404,
                messgae: '群组不存在'
            }
        } else if (code === 302) {
            ctx.body = {
                reasonCode: 302,
                message: '你已经是该群组成员'
            }
        } else if (code === 200) {
            ctx.body = {
                reasonCode: 200,
                message: '加入成功'
            }
        } else {
            ctx.body = {

            }
        }
    }).catch(err => {
        ctx.body = err;
    })
}


// 组内成员
exports.members = async (ctx, next) => {
    const { id } = ctx.request.query;
    const data = await Group.findOne({_id: id}).populate('members', { name: 1, avatar: 1});
    if (!data) {
        ctx.body = {
            reasonCode: 404,
            message: '群组不存在'
        }
    } else {
        ctx.body = {
            reasonCode: 200,
            members: data.members
        }
    }
}

// 退出组
exports.quit = async (ctx, next) => {
    const { id, groupId } = ctx.request.fields;
    const group = await Group.findOne({_id: groupId })
    const index = group.members.findIndex(_id => {
        return _id == id;
    });
    console.log(index);
    if (index > -1) {
        group.members.splice(index, 1);
        await group.save();
    }
    ctx.body = {
        reasonCode: 200,
        message: '已退出群组'
    }
}